//
//  TitleTextFieldCell.m
//  MoneyIndividual
//
//  Created by Tai Le on 12/30/13.
//  Copyright (c) 2013 Le Anh Tai. All rights reserved.
//

#import "TitleTextFieldCell.h"
#import "CalculatorView.h"

@implementation TitleTextFieldCell

#pragma mark - Init

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        [self initializeTitle];
        [self initializeLine];
        _textField = [self newTextField];
    }
    
    return self;
}

- (void)initializeTitle
{
    _titleLabel = [UILabel newNormalLabel];
    [_subView addSubview:_titleLabel];
}

- (InsetTextField*)newTextField
{
    InsetTextField *textField = [[InsetTextField alloc] initWithInsetLeft:0 insetRight:30];
    [textField setFont:FONT(FONT_SIZE_NORMAL)];
    [textField setBackgroundColor:[UIColor clearColor]];
    textField.delegate = self;
    textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    textField.returnKeyType = UIReturnKeyDone;
    textField.textAlignment = NSTextAlignmentRight;
    [_subView addSubview:textField];
    
    return textField;
}

- (void)initializeLine
{
    _lineTopView = [[UIView alloc] init];
    _lineTopView.backgroundColor = COLOR_ORANGE;
    [self.contentView addSubview:_lineTopView];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    _lineTopView.frame = RECT(0, self.contentView.height - 3, self.contentView.width, 3);
    
    _subView.frame = RECT(0, 0, self.contentView.width, self.contentView.height - _lineTopView.height);
    
    _titleLabel.frame = RECT(PADDING, 0, _subView.width/3, _subView.height);
    CGSize expectedTitleSize = [_titleLabel.text sizeWithFont:_titleLabel.font
                                            constrainedToSize:_titleLabel.frame.size
                                                lineBreakMode:_titleLabel.lineBreakMode];
    _titleLabel.width = expectedTitleSize.width;
    
    _textField.frame = RECT(_titleLabel.right + PADDING, 0, _subView.width - _titleLabel.right - PADDING, _subView.height);
    
}

@end
